
//
//  XMPicCell.m
//  PictureShow
//
//  Created by new-1020 on 2017/1/17.
//  Copyright © 2017年 new-1020. All rights reserved.
//

#import "XMPicCell.h"
#import "UIImageView+WebCache.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface  XMPicCell()<UIScrollViewDelegate>
@property(nonatomic,strong)UIProgressView * progressView;
@end

@implementation XMPicCell

-(UIProgressView *)progressView {
    if (_progressView == nil) {
        _progressView = [[UIProgressView alloc]init];
    }
    return _progressView;
}

-(UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.maximumZoomScale = 2.0;
        _scrollView.minimumZoomScale = 0.5;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UIImageView *)imageView {
    if(_imageView == nil){
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

-(void)setImage:(UIImage *)image {
    _image = image;
    [self resetView];
    CGFloat W = SCREEN_WIDTH;
    CGFloat H = W * (image.size.height / image.size.width) ;
    CGFloat offsetY = (SCREEN_HEIGHT - H ) * 0.5;
    self.imageView.frame = CGRectMake(0, 0, W, H);

    if (H < SCREEN_HEIGHT) {
        self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, offsetY, 0);
    }else {
    }
    self.imageView.image = _image;
}

-(void)setImgUrl:(NSURL *)imgUrl {
    _imgUrl = imgUrl;
    [self resetView];
//    [[SDWebImageManager sharedManager].defaultImageCache i]
//    UIImage * image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey: [self.suolvUrl absoluteString]];
    UIImage * plackholdImg = [UIImage new]; //PlaceHolderImage;
//    if (image) {
//        CGFloat W = SCREEN_WIDTH;
//        CGFloat H = W * (image.size.height / image.size.width) ;
//        CGFloat offsetY = (SCREEN_HEIGHT - H ) * 0.5;
//        self.imageView.frame = CGRectMake(0, 0, W, H);
//        if (H < SCREEN_HEIGHT) {
//            self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, offsetY, 0);
//        }else {
//            self.scrollView.contentSize = self.imageView.frame.size;
//        }
//        plackholdImg = image;
//    }
    [self.imageView sd_setImageWithURL:imgUrl placeholderImage:plackholdImg completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (error) {
            return;
        }
        CGFloat W = SCREEN_WIDTH;
        CGFloat H = W * (image.size.height / image.size.width) ;
        CGFloat offsetY = (SCREEN_HEIGHT - H ) * 0.5;
        self.imageView.frame = CGRectMake(0, 0, W, H);
        if (H < SCREEN_HEIGHT) {
            self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, offsetY, 0);
        }else {
            self.scrollView.contentSize = self.imageView.frame.size;
        }
    }];
}

-(void)resetView {
    // 重置图片，解决单元格重用问题
    self.scrollView.contentSize = CGSizeZero;
    self.scrollView.contentInset = UIEdgeInsetsZero;
    self.scrollView.contentOffset = CGPointZero;
    self.imageView.transform = CGAffineTransformIdentity;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

-(void)creatUI {
    UILongPressGestureRecognizer * longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self addGestureRecognizer:longP];
    [self.contentView addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageView];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.frame = [UIScreen mainScreen].bounds;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
    [self addGestureRecognizer:tap];
    [tap addTarget:self action:@selector(tap)];
}

-(void)longPress:(UILongPressGestureRecognizer *)longP {
    NSLog(@"%ld",longP.state);
    if (longP.state == UIGestureRecognizerStateBegan) {
        if (self.longPressBlock) {
            self.longPressBlock();
        }
    }
}

-(void)tap {
    if (self.tapBlock) {
        self.tapBlock();
    }
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGFloat offsetY = (SCREEN_HEIGHT - self.imageView.frame.size.height) * 0.5;
    CGFloat offsetX = (SCREEN_WIDTH  - self.imageView.frame.size.width) * 0.5;
    offsetY = (offsetY < 0) ? 0 : offsetY;
    offsetX = (offsetX < 0) ? 0 : offsetX;
    self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, offsetY, offsetX);
}

@end
